package com.windgo.controller.wx;

import com.windgo.bean.MarketOrderGoods;
import com.windgo.bean.MarketUser;
import com.windgo.bean.bo.OrderDeleteBo;
import com.windgo.bean.bo.WxCommentCreateBo;
import com.windgo.bean.bo.WxOrderPayBo;
import com.windgo.bean.common.BasePageInfo;
import com.windgo.bean.common.BaseRespVo;
import com.windgo.bean.common.CommonData;
import com.windgo.bean.vo.WxOrderDetailVo;
import com.windgo.bean.vo.WxOrderListVo;
import com.windgo.bean.bo.WxOrderSubmitBo;
import com.windgo.bean.vo.WxOrderSubmitVo;
import com.windgo.service.OrderService;
import com.windgo.service.WxCartService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author:ZhouXin
 * @Date:2022/10/14 14:45
 * @Description:
 **/
@RestController
@RequestMapping("wx/order")
public class WxOrderController {
    @Autowired
    OrderService orderService;

    @Autowired
    WxCartService wxCartService;


    /**
     * 订单确认收货后，添加评价
     *
     * @param wxCommentCreateBo
     * @return
     */
    @PostMapping("comment")
    public BaseRespVo wxComment(@RequestBody WxCommentCreateBo wxCommentCreateBo) {
        //从session域中获取用户信息
        Subject subject = SecurityUtils.getSubject();
        PrincipalCollection principals = subject.getPrincipals();
        MarketUser primaryPrincipal1 = (MarketUser) principals.getPrimaryPrincipal();

        Integer userId = primaryPrincipal1.getId();
        orderService.wxComment(wxCommentCreateBo, userId);
        return BaseRespVo.ok();
    }

    /**
     * 根据showType不同值，显示不同状态的订单
     * 0:显示所有订单
     * 1:显示未付款的
     * 2:显示代发货的
     * 3:显示待收货的
     * 4:显示待评价的
     *
     * @param info
     * @param showType
     * @return
     */
    @GetMapping("list")
    public BaseRespVo wxList(BasePageInfo info, Integer showType) {
        CommonData<WxOrderListVo> data = orderService.wxList(info, showType);
        return BaseRespVo.ok(data);
    }

    /**
     * 订单详情：根据orderId获取订单详情
     * 响应对应的可选操作，比如付款；申请退款
     *
     * @param orderId
     * @return
     */
    @GetMapping("detail")
    public BaseRespVo wxDetail(Integer orderId) {
        WxOrderDetailVo data = orderService.wxDetail(orderId);
        return BaseRespVo.ok(data);
    }


    /**
     * 根据id进行取消订单，订单状态码有101：未付款，改成102：用户取消
     *
     * @param orderDeleteBo
     * @return
     */
    @PostMapping("cancel")
    public BaseRespVo wxCancel(@RequestBody OrderDeleteBo orderDeleteBo) {
        Integer orderId = orderDeleteBo.getOrderId();
        orderService.wxCancel(orderId);
        return BaseRespVo.ok();
    }

    /**
     * 根据id进行申请退款，订单状态码....202：申请退款
     *
     * @param orderDeleteBo
     * @return
     */
    @PostMapping("refund")
    public BaseRespVo wxRefund(@RequestBody OrderDeleteBo orderDeleteBo) {
        Integer orderId = orderDeleteBo.getOrderId();
        orderService.wxRefund(orderId);
        return BaseRespVo.ok();
    }


    /**
     * 删除订单
     *
     * @param orderDeleteBo
     * @return
     */
    @PostMapping("delete")
    public BaseRespVo wxDelete(@RequestBody OrderDeleteBo orderDeleteBo) {
        Integer orderId = orderDeleteBo.getOrderId();
        orderService.wxDelete(orderId);
        return BaseRespVo.ok();

    }

    /**
     * 确认收货，修改状态码：401
     *
     * @param orderDeleteBo
     * @return
     */
    @PostMapping("confirm")
    public BaseRespVo wxConfirm(@RequestBody OrderDeleteBo orderDeleteBo) {
        Integer orderId = orderDeleteBo.getOrderId();
        orderService.wxConfirm(orderId);
        return BaseRespVo.ok();

    }

    /**
     * 确认收货后，商品评价时，商品信息的回显
     * 根绝orderId,goodsId一起去查找
     * 从order_goods表中获取数据
     *
     * @return
     */
    @GetMapping("goods")
    public BaseRespVo wxGoods(Integer orderId, Integer goodsId) {
        MarketOrderGoods data = orderService.wxGoods(orderId, goodsId);
        return BaseRespVo.ok(data);
    }

    /**
     * 购物车下单
     * @param wxOrderSubmitBo
     * @return
     */
    @PostMapping("submit")
    public BaseRespVo wxSubmit(@RequestBody WxOrderSubmitBo wxOrderSubmitBo){
        WxOrderSubmitVo data=wxCartService.wxSubmit(wxOrderSubmitBo);
        return BaseRespVo.ok(data);
    }

    /**
     * 用户付款
     * @param wxOrderPayBo
     * @return
     */
    @PostMapping("prepay")
    public BaseRespVo wxPrepay(@RequestBody WxOrderPayBo wxOrderPayBo){
        Integer orderId = wxOrderPayBo.getOrderId();
        orderService.wxPrepay(orderId);
        return BaseRespVo.ok();

    }
}
